package com.duochuang.verifycard.util;

import java.util.Random;

public class RandomStrUtil {

	public static void main(String[] args) {
		System.out.println(getStringRandom(8));
	}
	//生成随机用户名，数字和字母组成,  
	public static String getStringRandom(int length) {  
	      
	      
	    Random random = new Random();  
	    int tmp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
	    String val = String.valueOf((char)(random.nextInt(26) + tmp));
	    //参数length，表示生成几位随机数  
	    for(int i = 1; i < length; i++) {  
	          
	        String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
	        //输出字母还是数字  
	        if( "char".equalsIgnoreCase(charOrNum) ) {  
	            //输出是大写字母还是小写字母  
	            int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
	            val += (char)(random.nextInt(26) + temp);  
	        } else if( "num".equalsIgnoreCase(charOrNum) ) {  
	            val += String.valueOf(random.nextInt(10));  
	        }  
	    }  
	    return val;  
	}   

}
